package cn.itcast.pattern;

import lombok.extern.slf4j.Slf4j;

/**
 * className: TestGuardedObject
 * description:
 * date: 2023/3/8 0008-15:22
 * <p>
 * project: concurrent
 * package: cn.itcast.pattern
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
@Slf4j(topic = "c.TestGuardedObject")
public class TestGuardedObject {
    public static void main(String[] args) {

    }
}

class GuardedObject {
    private Object response;
    ;
    private final Object lock = new Object();

    public Object get() {
        synchronized (lock) {
            // 条件不满足则等待
            while (response == null) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return response;
        }
    }

    public void complete(Object response) {
        synchronized (lock) {
            // 条件满足，通知等待线程
            this.response = response;
            lock.notifyAll();
        }
    }
}
